package com.medical.admin.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.RoleDto;
import com.medical.admin.sys.entity.Permission;
import com.medical.admin.sys.entity.Role;
import com.medical.admin.sys.mapper.PermissionMapper;
import com.medical.admin.sys.mapper.RoleMapper;
import com.medical.admin.sys.service.RoleService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.uitils.BeanPageUtils;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Supplier;

/*
 * @Date : 2024/4/26 16:37
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Transactional(rollbackFor = RuntimeException.class, timeout = 5)
    @Override
    public Integer addRole(RoleDto roleDto) {

        // 查询是否有相同的角色
        LambdaQueryWrapper<Role> eq = new QueryWrapper<Role>().lambda().eq(Role::getMarRoleName, roleDto.getMarRoleName());
        if (ObjectUtil.isNotEmpty(roleMapper.selectOne(eq))) {
            throw new ServiceException(ResponseCode.ROLE_IS_HAVE);
        }

        Role role = BeanConvertUtils.convertTo(roleDto, Role::new);
        // 添加角色
        roleMapper.insert(role);
        // 给角色添加权限
        List<Permission> permissions = roleDto.getPermission();
        ArrayList<Integer> integers = new ArrayList<>();
        permissions.forEach(permission -> integers.add(permission.getPermissionId()));

        return permissionMapper.batchInsert(role.getMarRoleId(), integers);
    }

    @Override
    public Page<RoleDto> list(String roleName, Integer pageNum, Integer pageSize) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        if (roleName != null && !roleName.isEmpty()) {
            lambda.like(Role::getMarRoleName, roleName);
        }
        page = roleMapper.selectPage(page, lambda);
        Page<RoleDto> iPage = new Page<>();

        Supplier<RoleDto> targetSupplier = RoleDto::new;
        return BeanPageUtils.convertPage(page, iPage, targetSupplier);
    }

    @Override
    public Integer modStatusById(Integer roleId, Integer roleStatus) {
        Role role = new Role();
        role.setMarRoleId(roleId);
        role.setMarRoleStatus(roleStatus);
        return roleMapper.updateById(role);
    }

    @Transactional(rollbackFor = RuntimeException.class, timeout = 5)
    @Override
    public Integer mod(RoleDto roleDto) {
        Role role = BeanConvertUtils.convertTo(roleDto, Role::new);
        //判断当前角色名是否存在(排除当前角色的前提)
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda()
                .eq(Role::getMarRoleName, roleDto.getMarRoleName());
        if (ObjectUtil.isNotEmpty(roleMapper.selectOne(lambda)) && !Objects.equals(role.getMarRoleId(), roleDto.getMarRoleId())) {
            throw new ServiceException(ResponseCode.ROLE_IS_HAVE);
        }

        // 修改角色
        roleMapper.updateRole(role);

        //删除该用户的所有权限
        permissionMapper.deletePermissions(role.getMarRoleId());
        // 给角色添加权限
        List<Permission> permissions = roleDto.getPermission();
        ArrayList<Integer> integers = new ArrayList<>();
        permissions.forEach(permission -> integers.add(permission.getPermissionId()));

        return permissionMapper.batchInsert(role.getMarRoleId(), integers);
    }

    @Transactional(rollbackFor = RuntimeException.class,timeout = 5)
    @Override
    public Integer delBatch(Integer[] roleIds) {

        return roleMapper.delBatch(Arrays.asList(roleIds));
    }

    @Override
    public Integer del(Integer roleId) {
        LambdaUpdateWrapper<Role> eq = new LambdaUpdateWrapper<Role>().set(Role::getMarRoleStatus, 0).eq(Role::getMarRoleId, roleId);
        return roleMapper.update(eq);
    }

    @Override
    public RoleDto selectById(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        return BeanConvertUtils.convertTo(role, RoleDto::new);
    }
}
